package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.partrecord;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.bean.PartBean;
import st.business.tepth.business.bean.ProgramBean;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.MultipleItemEntity;
import st.shadow.library.ui.recycler.MultipleViewHolder;

/**
 * @author zhangyu
 * @date 2019/7/1
 */
public class PartRecordAdapter extends BaseMultiItemQuickAdapter<MultipleItemEntity,
        MultipleViewHolder> {
    //未确认
    public static final int TYPE_UN_CONFIRM = 100;
    //已确认
    public static final int TYPE_CONFIRM = 101;


    public PartRecordAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(TYPE_UN_CONFIRM, R.layout.item_part_record_type_1);
        addItemType(TYPE_CONFIRM, R.layout.item_part_record_type_0);
    }

    @Override
    protected void convert(MultipleViewHolder holder, MultipleItemEntity entity) {
        int status = entity.getField("status");
        holder.setVisible(R.id.tv_operation, status < 3);
        ProgramBean programBean = entity.getField(ItemFields.ITEM_OBJ);
        holder.setText(R.id.tv_apply_person, programBean.getMainPerson() == null ?
                "无" : programBean.getMainPerson().getUser_name());
        holder.setText(R.id.tv_choose_action, programBean.getAction_name());
        PartBean part = programBean.getPart();
        holder.setText(R.id.tv_apply_goods, part == null ? "无" : part.getMaterial_name());
        holder.setText(R.id.tv_part_code, part == null ? "无" : part.getMaterial_no());
        holder.setText(R.id.tv_goods_count, part == null ? "1" : String.valueOf(part.getVal()));
        holder.setText(R.id.tv_unit, part == null ? "无" : part.getUnit());
        String positionName = programBean.getPositionName();
        holder.setText(R.id.tv_place, positionName == null ? "无" : positionName);
        switch (entity.getItemType()) {
            case TYPE_UN_CONFIRM:
                holder.addOnClickListener(R.id.tv_choose_action);
                holder.addOnClickListener(R.id.tv_apply_goods);
                holder.addOnClickListener(R.id.tv_part_code);
                holder.addOnClickListener(R.id.tv_goods_count);
                holder.addOnClickListener(R.id.tv_place);
                break;
            case TYPE_CONFIRM:
                break;
        }
        holder.addOnClickListener(R.id.tv_operation);
    }

    public boolean hasNoPart() {
        List<MultipleItemEntity> all = getData();
        for (MultipleItemEntity entity : all) {
            ProgramBean diagnoseBean = entity.getField(ItemFields.ITEM_OBJ);
            if (diagnoseBean.getPart() == null) {
                return true;
            }
        }
        return false;
    }

    public boolean hasNotCommitData() {
        List<MultipleItemEntity> all = getData();
        for (MultipleItemEntity entity : all) {
            if (entity.getItemType() == TYPE_UN_CONFIRM) {
                return true;
            }
        }
        return false;
    }

    public static List<MultipleItemEntity> convertList(List<DiagnoseBean> tempList,
                                                       int status) {
        List<MultipleItemEntity> list = new ArrayList<>();
        if (tempList != null) {
            for (DiagnoseBean diagnoseBean : tempList) {
                List<ProgramBean> programBeanList = diagnoseBean.getPlanList();
                if (programBeanList != null) {
                    for (ProgramBean programBean : programBeanList) {
                        //已确认的就不管是否是推荐还是自己添加的
                        list.add(MultipleItemEntity.builder()
                                .setItemType(TYPE_CONFIRM)
                                .setField("status", status)
                                .setField(ItemFields.ITEM_OBJ, programBean)
                                .build());
                    }
                }
            }
        }
        return list;
    }
}
